package com.invengo.module_check.ui.fragment.first_check;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.module_check.BR;
import com.invengo.module_check.R;
import com.invengo.module_check.data.repsitory.business.BusinessRepsitory;
import com.invengo.module_check.ui.fragment.first_check.item.FirstCheckItemViewModel;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.constant.Constants;
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class FirstCheckViewModel extends BaseViewModel<BusinessRepsitory> {

//    public RFIDCallBackListener rfidService = new RFIDCallBackListener(AnalyzeImpl.TO_SHELF, tagInfo -> onScanResult(tagInfo), state -> isReading.set(state));
public String shelfTip = "共 <span style=\"color:#5DBB80; font-weight:bold;\">%d</span> 层";

    public BindingRecyclerViewAdapter adapter = new BindingRecyclerViewAdapter();
    public ObservableList<FirstCheckItemViewModel> shelfList = new ObservableArrayList<>();
    public ItemBinding<FirstCheckItemViewModel> shelfItemBinding = ItemBinding.of(BR.viewModel, R.layout.item_first_check);

    public FirstCheckViewModel(@NonNull Application application) {
        super(application, BusinessRepsitory.getInstance());
//        registerRFID(rfidService);
        Messenger.getDefault().register(this, Constants.TOKEN_DIALOG_2_FIRST_CHECK, ShelfBean.class, bean -> {
            if (currentPosition != -1)
                shelfList.get(currentPosition).updateNewBook(bean);
            updateFistPos(bean);
        });
        Messenger.getDefault().register(this, Constants.TOKEN_REGISTER_AGAIN, String.class, value -> {
            registerEvent("FirstCheckFragment");
        });
    }

    private String startShelfCode, endShelfCode;

    public void updateShelfList(List<ShelfBean> beans, String startShelfCode, String endShelfCode) {
        this.startShelfCode = startShelfCode;
        this.endShelfCode = endShelfCode;
        shelfList.clear();
        for (int i = 0; i < beans.size(); i++) {
            shelfList.add(new FirstCheckItemViewModel(this, beans.get(i), i));
        }
        ui.tipEvent.postValue(String.format(shelfTip,shelfList.size()));
    }



    private void getErrorShelfList() {
        model.getErrorShelf(startShelfCode, endShelfCode)
                .compose(RxUtils.applySchedulersLoading(getLifecycleProvider(), this))
                .subscribe(shelfBeans -> {
                    shelfList.clear();
                    if (!shelfBeans.isEmpty()) {
                        for (int i = 0; i < shelfBeans.size(); i++) {
                            shelfList.add(new FirstCheckItemViewModel(this, shelfBeans.get(i), i));
                        }
                    }
                    ui.tipEvent.postValue(String.format(shelfTip,shelfList.size()));
                });
    }


    private void updateFistPos(ShelfBean shelfBean) {
        List<ShelfBean> beans = new ArrayList<ShelfBean>();
        beans.add(shelfBean);
        model.updateFirstBook(beans)
                .compose(RxUtils.applySchedulersToast(getLifecycleProvider(), this))
                .subscribe(updateResult -> {
                    getErrorShelfList();
                });
    }


    public UIChangeObservable ui = new UIChangeObservable();


    public class UIChangeObservable {
        public SingleLiveEvent<String> tipEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<ShelfBean> changeBookEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<Boolean> nextEvent = new SingleLiveEvent<>();
    }

    public int getPosition(ItemViewModel item) {
        return shelfList.indexOf(item);
    }

    private int currentPosition = -1;

    public void changeFistBook(FirstCheckItemViewModel itemViewModel) {
        currentPosition = getPosition(itemViewModel);
        removeEvent();
        ui.changeBookEvent.postValue(itemViewModel.shelfBean.getValue());
    }

    public BindingCommand updateList = new BindingCommand(() -> {
        createUpdateTask();
    });

    private void createUpdateTask() {
        model.createupdatetask(startShelfCode, endShelfCode)
                .compose(RxUtils.applySchedulersToast(getLifecycleProvider(), this))
                .subscribe(updateResult -> {
                    ui.nextEvent.postValue(updateResult.getAffactNum() > 0);
                }, throwable -> {
                    ui.nextEvent.postValue(false);
                });
    }
}
